<script setup lang="ts">
import { ref, computed } from'vue';
import { getCategoryLevelApi } from '@/apis/category';
import type { Category } from '@/types/Category';
import { onLoad } from'@dcloudio/uni-app';


// 接收数据
const categoryData = ref<Category[]>([])
const activeIndex = ref(0)
// 获取分类数据
const getCategoryLevelData = async() =>{
  const res = await getCategoryLevelApi()
  categoryData.value = res.data;
  console.log(categoryData)
}

const subCategoryList = computed(()=>{
  return categoryData.value[activeIndex.value]?.children || [];
})

// 组件加载时执行
onLoad(()=>{
  getCategoryLevelData()
})
</script>

<template>
  <view class="content">
    <!-- <view class="search">
    hhhh
    </view> -->

    <view class="category">
      <scroll-view scroll-y class="scroll_left">
        <view v-for="(item, index) in categoryData" :key="item.id" class="item" :class="{ active: index === activeIndex }" @tap="activeIndex = index">
          <text class="name"> {{ item.name }} </text>
        </view>
      </scroll-view>
      <!-- 右侧：二级分类 -->
      <scroll-view scroll-y class="scroll_right">
        <!-- 内容区域 -->
        <view class="panel" v-for="item in subCategoryList" :key="item.id">
          <view class="title">
            <text class="name">{{ item.name }}</text>
            <!-- <navigator class="more" hover-class="none" :url="`/pages/goods/goods?id=${item.id}`"><text class="icon-youjiantou1"></text></navigator> -->
          </view>
          <view class="section">
            <navigator
              v-for="el in item.children"
              :key="el.id"
              class="goods"
              hover-class="none"
              :url="`/pagesSearch/searchGoods/searchGoods?categoryId=${el.id}`"
            >
              <image
                class="image"
                :src="el.icon?el.icon:'/static/logo.png'"
              ></image>
              <view class="name">{{ el.name }}</view>
            </navigator>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<style lang="scss">
page {
  height: 100%;
  overflow: hidden;
}

.content{
  height: 100%;
  display: flex;
  flex-direction: column;
}
.search{
  height: 70rpx;
  width: 100%;
  background-color: aqua;
}

.category{
  flex: 1;
  min-height: 400rpx;
  display: flex;
}

.scroll_left{
  overflow: hidden;
  width: 180rpx;
  flex: none;
  background-color: #f1f1f1d2;

  .item {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 96rpx;
    font-size: 26rpx;
    color: #595c63;
    position: relative;
    &::after {
      content: '';
      position: absolute;
      left: 42rpx;
      bottom: 0;
      width: 96rpx;
    }
  }
  .active {
    background-color: #fff;
    &::before {
      content: '';
      position: absolute;
      left: 0;
      top: 25%;
      width: 10rpx;
      height: 50%;
      border-radius: 10rpx;
      background-color: red;
    }
  }
}

.scroll_left .item:last-child::after,
.scroll_left .active::after {
  display: none;
}

.scroll_right{
  background-color: #fff;

  .panel{
    margin: 5rpx 20rpx 0;
  }

  .title {
    height: 60rpx;
    line-height: 60rpx;
    color: #333;
    font-size: 28rpx;
    font-weight: 600;
    border-bottom: 1rpx solid #ededee;
    .more {
      float: right;
      padding-left: 10rpx;
      // font-size: 24rpx;
      // color: #999;
    }
  }

  .section {
    width: 100%;
    display: flex;
    flex-wrap: wrap;
    padding: 20rpx 0;
    .goods {
      width: 120rpx;
      margin: 0rpx 30rpx 20rpx 0;
      text-align: center;
      &:nth-child(3n) {
        margin-right: 0;
      }
      image {
        width: 120rpx;
        height: 120rpx;
      }
      .name {
        padding: 5rpx;
        font-size: 22rpx;
        color: #333;
      }
      .number {
        font-size: 24rpx;
        margin-left: 2rpx;
      }
    }
  }
}
</style>